<{*发布上新/修改*}>
<{include file="Toy/toy_top.html"}>
<link rel="stylesheet" href="<{$preStaticUrl}>Wap/v2/css/mui.picker.min.css?v=<{$resVersion}>" />
<script src="<{$preStaticUrl}>Wap/v2/js/mui.min.js"></script>
<script src="<{$preStaticUrl}>Wap/v2/js/mui.picker.min.js"></script>
<script src="<{$preStaticUrl}>Wap/v2/js/ningbo.js"></script>
<style>
	iframe{position: fixed;z-index: 10002;top: 0;background-color: white;display: none;border:none;}
	.input{border: 1px solid #e5e5e5;height: 0.6rem;border-radius: 0.12rem;	font-size: 0.34rem;line-height: 0.34rem}
</style>
    </head>
    <body>
    	<div id="wrap" class="shelves">
			<{include file="Toy/toy_nav.html"}>

			<form id="form">
				<input type="hidden" name="id" value="<{$obj.id|default:0}>" />
				<div class="container">
					<p class="tip">温馨提示：上传的玩具需要通过审核后，才能在首页中显示。</p>
					<div class="label clearfix">
						宝贝描述
						<span class="word"><i class="remain-1">0</i>/20</span>
					</div>
					<textarea id="desc" name="desc" placeholder="简单描述您宝贝的颜色和尺寸。" required="required"><{$obj.desc}></textarea>

					<div class="label clearfix">
						所在地区 <input type="text" id="position" value="<{$obj.position}>" class="input" name="position" readonly />
					</div>

					<div class="label clearfix">
						物品价值 <input type="tel" class="input" name="value"  value="<{$obj.value}>" />
					</div>

					<div class="label">图片上传 <em>(需上传3-5张)</em></div>

					<div class="upload-box">
						<div class="upload" id="listPic" style="border-color: darkgray;height: 1.27rem;">
							<{if $obj.headpic}>
							<div class="item" style="background:url(<{$obj.headpic}>) no-repeat;">
								<a href="javascript:void(0);" class="delete"></a>
								<input type="hidden" name="headpic" value="<{$obj.headpic}>" class="file" />
							</div>
							<{/if}>
						</div>
						<{foreach from=$album item=pic}>
						<{if strlen($pic) gt 0}>
							<div class="item" style="background:url(<{$pic}>) no-repeat;"><a href="javascript:void(0);" class="delete"></a><input type="hidden" name="album[]" value="<{$pic}>" class="file"></div>
						<{/if}>
						<{/foreach}>
						<div class="upload" id="album" <{if count($album) gt 3}>style="display:none"<{/if}> >
							<!--<input type="file" name="file" id="up" accept="image/*" >-->
						</div>
					</div>
					<div class="label clearfix">
						意向说明 <em>(提高兑换成功率)</em>
						<span class="word"><i class="remain-2">0</i>/50</span>
					</div>
					<textarea id="intention" name="intention" placeholder="让其他宝妈知道您想兑换的玩具，能提高兑换成功率哦~" required><{$obj.intention}></textarea>
				</div>
				<a href="javascript:void(0);" class="submit">
					<{if $pointOK eq 1}>上&nbsp;&nbsp;架<{else}>积分不足<{/if}>
				</a>
			</form>
    		<{if $obj.id gt 0}>
		<{else}>
		    <{if $pointOK eq 1}>
				<p class="notice">(今日还可上传<span id="times" ><{$leftTimes}></span>次)</p>
		    <{else}>
				<p class="notice" onclick="location.href='<{u('Point/rule')}>'">(如何获取积分)</p>
			<{/if}>
		<{/if}>

		<div id="shareBox" >
			<div id="shareTitle">上传成功！</div>
			<div id="shareTip">分享到朋友圈，让更多的朋友参与，能提高交换成功率哦！</div>
    	</div>


			
		</div>
			<iframe id="cutImage" src="<{u('Common/cutImage?type=IMG_TOY_LIST')}>" ></iframe>
			<iframe id="resizeImage" src="<{u('Common/resizeImage?type=IMG_TOY_ALBUM')}>" ></iframe>

    	<script src="<{$preStaticUrl}>Wap/js/jquery-2.1.4.min.js"></script>
    	<script src="<{$preStaticUrl}>Wap/toy/js/index.js"></script>

		<script src="<{$preStaticUrl}>js/jquery-1.11.0.js?v=<{$resVersion}>"></script>
		<script src="<{$preStaticUrl}>js/jquery.ajaxfileupload.js"></script>
    	<script>
    		$(function(){
				load();

				$('#listPic').on('click',function(){
					$('#cutImage').show();
					$(document.getElementById('cutImage').contentWindow.document.getElementById('file')).click()
				});
				$('#album').on('click',function(){
					$('#resizeImage').show();
					$(document.getElementById('resizeImage').contentWindow.document.getElementById('file')).click()
				});
    			/*
			     * 剩余字数统计
			     */
			    function statInputNum(textArea,numItem,max) {
			        var  curLength;
			        textArea[0].setAttribute("maxlength", max);
			        curLength = textArea.val().length;
			        numItem.text(curLength);
			        textArea.on('input propertychange', function () {
			            var _value = $(this).val().replace(/\n/gi,"");
			            numItem.text(_value.length);
			        });
			    }
			    
		        statInputNum($("#desc"),$(".remain-1"),20);
		        statInputNum($("#intention"),$(".remain-2"),50);

				//删除图片
				$(document).on("click",".delete",function(){
					$(this).parent().remove();
					var curLen = $(".upload-box>.item").size();
					if(curLen<4){
						$('#album').show();
					}

				});

				var hasPush=false;
				//上架
				$(".submit").click(function(){

					if(hasPush){return false;}
					<{if $pointOK neq 1 or $leftTimes lt 1}>
						return false;
					<{/if}>

					var uploadImg = [];
					$(".file").each(function(idx,item){
						uploadImg.push($(item).val())
					})

					var desc = $("#desc").val();
					if(desc==""){
						alert("请填写您的宝贝描述");
						return
					}
					if(uploadImg.length==0){
						alert("请上传图片");
						return
					}
					var intention = $("#intention").val();
					if(intention==""){
						alert("请填写意向说明");
						return
					}
						hasPush=true;
					var data=$('#form').serialize();
					$.ajax({
						type:"post",
						url:"<{u('publish')}>",
						dataType: 'json',
						data: data,
						success: function(data){
							hasPush=false;
							if(data.status==0){
								if(typeof data.tip !='undefined'){
									tip(data.tip);
								}
								$('#shareBox').show().on('click',function(){
									location.href="<{u('myPrefer')}>";
								})
							}else{
								tip(data.msg)
							}
						}
					});
				})
				$('#cutImage,#resizeImage').css({'height':document.body.clientHeight+"px",'width':document.body.clientWidth+"px"})
			});

			function newImg(src){
				var img = '<div class="item" style="background:url('+src+') no-repeat;">'+
						'<a href="javascript:void(0);" class="delete"></a>'+
						'<input type="hidden" name="album[]" value="'+src+'" class="file"/>'+
						'</div>';
				$("#album").before(img);

				var curLen = $(".upload-box>.item").size();
				if(curLen>=4){
					$('#album').hide();
				}

			}

			function editImg(src){
				var img = '<div class="item" style="background:url('+src+') no-repeat;">'+
						'<a href="javascript:void(0);" class="delete"></a>'+
						'<input type="hidden" name="headpic" value="'+src+'" class="file"/>'+
						'</div>';
				$("#listPic").html(img);
			}

			function closeIframeCut(){
				$('#cutImage').hide();
			}
			function closeIframeResize(){
				$('#resizeImage').hide();
			}

			var close=0;
			function closeLoad(){
				close++;
				if(close>=2){
					layer.closeAll();
				}
			}
			function load(){
				layer.open({type: 2,shadeClose:false});
			}


			//区域选择
			var cityPicker = new mui.PopPicker({
				layer: 2
			});

			cityPicker.setData(nbData);

			var _getParam = function(obj, param) {
				return obj[param] || '';
			};
			$("#position").click(function(){
				var _self = this;
				cityPicker.show(function(items) {
					var text = _getParam(items[0], 'text') + "-" + _getParam(items[1], 'text');
					$(_self).val(text);

				});
			})

		</script>
	</body>
</html>
